﻿using System.Threading.Tasks;
using CrateMayhem.Common.Services;
using CrateMayhem.WinRT.Common;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;

namespace CrateMayhem.WinRT.Controls
{
    public class AutoCloseDialog
    {
        private readonly IAutoCloseDialogService autoCloseDialogService;

        public AutoCloseDialog(IAutoCloseDialogService autoCloseDialogService)
        {
            this.autoCloseDialogService = autoCloseDialogService;
        }

        private static Brush DialogForegroundBrush
        {
            get { return Resources.GameStyles.Get<Brush>("DialogForegroundBrush"); }
        }

        private static Brush DialogBackgroundBrush
        {
            get { return Resources.GameStyles.Get<Brush>("DialogBackgroundBrush"); }
        }

        private static Style GameMessageTextBlockStyle
        {
            get { return Resources.GameStyles.Get<Style>("GameMessageTextBlockStyle"); }
        }

        private static int AutoCloseDialogWidth
        {
            get { return Resources.GameStyles.Get<int>("AutoCloseDialogWidth"); }
        }

        private static int AutoCloseDialogHeight
        {
            get { return Resources.GameStyles.Get<int>("AutoCloseDialogHeight"); }
        }

        public void ShowDialog(string message, int delayBeforeClose)
        {
            var popup = CreatePopup(message);
            popup.IsOpen = true;

            var dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;

            Task.Factory.StartNew(() =>
            {
                Task.Delay(delayBeforeClose).ContinueWith(t =>
                {
                    dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        popup.IsOpen = false;
                        autoCloseDialogService.Close();
                    });
                });
            });
        }

        private static Popup CreatePopup(string message)
        {
            var window = CoreWindow.GetForCurrentThread();
            var bounds = Window.Current.Bounds;
            var width = double.IsNaN(window.Bounds.Width) ? 500 : window.Bounds.Width;
            var height = double.IsNaN(window.Bounds.Height) ? 300 : window.Bounds.Height;

            var textBlock = new TextBlock {Text = message, Style = GameMessageTextBlockStyle};
            var border = new Border
                {
                    BorderThickness = new Thickness(3), 
                    BorderBrush = DialogForegroundBrush, 
                    Background = DialogBackgroundBrush,
                    Child = textBlock
                };

            return new Popup
                {
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                    Width = width,
                    Height = height,
                    HorizontalOffset = (bounds.Width - AutoCloseDialogWidth)/2,
                    VerticalOffset = (bounds.Height - AutoCloseDialogHeight)/2,
                    Child = border
                };
        }
    }
}
